package com.dataseek.iot.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dataseek.iot.product.entity.PdtAttrRel;
import com.dataseek.iot.product.entity.PdtAttributeDic;
import com.dataseek.iot.product.entity.PdtEventRel;
import com.dataseek.iot.product.entity.PdtFuncRel;
import com.dataseek.iot.product.mapper.PdtAttrRelMapper;
import com.dataseek.iot.product.mapper.PdtAttributeDicMapper;
import com.dataseek.iot.product.mapper.PdtEventRelMapper;
import com.dataseek.iot.product.mapper.PdtFuncRelMapper;
import com.dataseek.iot.product.service.AttrService;
import com.dataseek.iot.product.vo.AttrVo;
import com.dataseek.iot.toolikit.common.enums.RspStatusEnum;
import com.dataseek.iot.toolikit.common.service.IDServiceHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @discription:
 * @author: Freya.Yan
 * @date: 2022/1/11
 */
@Service
@Slf4j
public class AttrServiceImpl implements AttrService {

    @Autowired
    private IDServiceHelper idServiceHelper;


    @Autowired
    private PdtAttributeDicMapper pdtAttributeDicMapper;

    @Autowired
    private PdtAttrRelMapper pdtAttrRelMapper;

    @Autowired
    private PdtFuncRelMapper pdtFuncRelMapper;

    @Autowired
    private PdtEventRelMapper pdtEventRelMapper;


    @Override
    public AttrVo queryAttr(String attName, Integer num, Integer size) {

        AttrVo attrVo = new AttrVo();
        QueryWrapper<PdtAttributeDic> qw = new QueryWrapper<>();
        if (attName != null && !attName.isEmpty()) {
            qw.like("att_name", attName);
        }
        IPage<PdtAttributeDic> page = new Page<>(num, size);
        List<PdtAttributeDic> mapList = pdtAttributeDicMapper.selectPage(page, qw).getRecords();
        attrVo.setPage(num);
        attrVo.setSize(size);
        attrVo.setTotal(page.getTotal());
        attrVo.setList(mapList);
        return attrVo;
    }


    @Override
    public Integer insertAttr(PdtAttributeDic pdtAttributeDic) {
        int resultCode = 0;
        long id = idServiceHelper.nextId();
        pdtAttributeDic.setId(id);
        pdtAttributeDic.setStatus("active");
        String code = pdtAttributeDic.getAttName();
        QueryWrapper<PdtAttributeDic> qw = new QueryWrapper<>();
        qw.eq("att_name", code);
        Integer count = pdtAttributeDicMapper.selectCount(qw);
        if (count == 0) {
            // pdtAttributeDic.setTenantCode(tenantCode);
            resultCode = pdtAttributeDicMapper.insert(pdtAttributeDic);
        } else {
            resultCode = RspStatusEnum.FAIL.getCode();
        }
        return resultCode;

    }

    @Override
    public Integer updateAttr(PdtAttributeDic pdtAttributeDic) {
        int resultCode = pdtAttributeDicMapper.updateById(pdtAttributeDic);
        return resultCode;
    }

    @Override
    public Integer deleteAttr(List<Long> tenantIds) {
        Integer resultCode = 0;
        Integer count = 0;
        for (int i = 0; i < tenantIds.size(); i++) {
            long id = tenantIds.get(i);
            PdtAttributeDic pdtAttributeDic = pdtAttributeDicMapper.selectById(id);
            String attrCode = pdtAttributeDic.getAttCode();
            QueryWrapper<PdtAttrRel> qw = new QueryWrapper<>();
            qw.eq("att_code", attrCode);
            QueryWrapper<PdtFuncRel> qwFunc = new QueryWrapper<>();
            qwFunc.eq("att_code", attrCode);
            QueryWrapper<PdtEventRel> qwEvent = new QueryWrapper<>();
            qwEvent.eq("att_code", attrCode);
            count = count + pdtAttrRelMapper.selectCount(qw) + pdtFuncRelMapper.selectCount(qwFunc)+pdtEventRelMapper.selectCount(qwEvent);
        }
        if (count > 0) {
            resultCode = RspStatusEnum.FAIL.getCode();
        } else {
            resultCode = pdtAttributeDicMapper.deleteBatchIds(tenantIds);
        }
        return resultCode;
    }

}
